<?php include_once VIEWPATH.'common/dialog.php'; ?>

<!-- 日期组件 -->
<link href="<?php echo FONT_PATH.'static/bootstrap/';?>css/bootstrap-datetimepicker.min.css" rel="stylesheet">
<script src="<?php echo FONT_PATH.'static/bootstrap/';?>js/bootstrap-datetimepicker.js"></script>
<!-- 日期组件 -->

<script type="text/javascript">
    function on_submit(){
        var name = $("#name").val();
        var desc = $("#desc").val();
        var short_desc = $("#short_desc").val();
        var num = $("#num").val();
        var code = $("#code").val();
        var prize_num = $("#prize_num").val();
        var receive_action = $("#receive_action").val();
        var finish_action = $("#finish_action").val();
        if(!name){ alert("名称不能为空"); return false; }
        if(!short_desc){ alert("断描述不能为空"); return false; }
        if(!desc){ alert("描述不能为空"); return false; }
        if(!code){ alert("任务码不能为空"); return false; }
        if(short_desc.length>20){ alert("描述不能超过20字"); return false; }
        if(desc.length>100){ alert("描述不能超过一百字"); return false; }
        if(!receive_action){ alert("领取任务操作不能为空"); return false; }
        if(!finish_action){ alert("完成任务操作不能为空"); return false; }
        if(!num){ alert("周期内循环次数不能为空"); return false; }
        if(!prize_num){ alert("奖品发放数量不能为空"); return false; }
        $('form').submit();
    }
    $("#edit_task").on("hidden.bs.modal", function() {
        $(this).removeData("bs.modal");
    });
//    $(function(){
//        $('#pid').click(function(){
//            alert(111);
//        })
//    })
//    function get_pid(obj){
//
//        var pid = $(obj).val();
//        if(pid == 0){
//            $('#finish_action').find("option[value='complete_subtask']").removeAttr('disabled');
//            $('#finish_action').val('complete_subtask');
//            $('#finish_action').attr('disabled','disabled');
//
//        }else{
////            alert(pid);
//            $('#finish_action').removeAttr('disabled');
//            $('#finish_action').find("option[value='complete_subtask']").attr('disabled','disabled');
//        }
//    }
    function get_select_desc(obj,type){
        var val_t = $(obj).val();
        var desc=$('#finish_action').find("option[value='"+val_t+"']").attr('data-desc');
        if(type == 1){
            $('.ling_desc').html(desc);
        }else{
            $('.wan_desc').html(desc);
        }
    }
</script>

<div class="tab-pane active" id="horizontal-form">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true" aria-label="Close">×</button>
        <h5 class="modal-title"><?php echo $pageTitle;?></h5>
    </div>
    <form class="form-horizontal" name="form" id="form" action="index.php?c=<?php echo $_REQUEST['c'];?>&m=<?php echo $_REQUEST['m'];?>" name="form" method="post" enctype="multipart/form-data">
        <input type="hidden" name="id"  id="id" value="<?php echo $res['id']; ?>"/>
        <input type="hidden" name="submits" value="1"/>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">任务等级<span class="b_red">*</span></label>
            <div class="col-sm-4">

                <select name="label[pid]" id="pid" class="form-control" onchange="get_pid(this)">
                    <option value="-1" <?php echo $res['pid'] == -1 ? "selected" : "";?>>独立任务</option>
                    <option value="0" <?php echo $res['pid'] == 0 ? "selected" : "";?>>主任务</option>
                    <option disabled>↓↓↓主任务列表↓↓↓</option>

                    <?php foreach($master_task as $k=>$v){ ?>
                        <option value="<?php echo $v['id'];?>" <?php echo $res['pid'] == $v['id'] ? "selected" : "";?>><?php echo $v["name"];?></option>
                    <?php
                        }
                        unset($k,$v);
                    ?>
                </select>
            </div>
            <div class="col-sm-6">
                <p class="help-block">独立任务不可创建子任务，主任务可创建子任务，如果为主任务中的一个子任务，请选择主任务列表下面的指定主任务</p>
            </div>
        </div>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">任务名称<span class="b_red">*</span></label>
            <div class="col-sm-6">
                <input type="text" name="label[name]" id="name" value="<?php echo $res["name"];?>" class="form-control" id="displayorder" placeholder="任务名称">
            </div>
            <div class="col-sm-2">
                <p class="help-block">不能为空</p>
            </div>
        </div>
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">短描述<span class="b_red">*</span></label>
            <div class="col-sm-5">
                <input type="text" name="label[short_desc]" id="short_desc" value="<?php echo $res["short_desc"];?>" class="form-control" id="displayorder" placeholder="短描述">
            </div>
            <div class="col-sm-5">
                <p class="help-block">短描述，不能为空，用来在积分和零钱列表展示</p>
            </div>
        </div>


        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">任务排序</label>
            <div class="col-sm-2">
                <input type="text" name="label[sort]" id="sort" value="<?php echo empty($res['sort'])?0:$res['sort'];?>" class="form-control" id="displayorder" placeholder="奖品发放数量">
            </div>
            <div class="col-sm-8">
                <p class="help-block">任务排序，主要关系到接子任务的先后顺序，最小的数的任务先被接……</p>
            </div>
        </div>
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">任务描述<span class="b_red">*</span></label>
            <div class="col-sm-6">
                <textarea name="label[desc]"  id="desc" cols="50" rows="3" class="form-control"><?php echo $res['desc']; ?></textarea>

            </div>
            <div class="col-sm-2">
                <p class="help-block">不能为空</p>
            </div>
        </div>


        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">领任务操作<span class="b_red">*</span></label>
            <div class="col-sm-4">

                <select name="label[receive_action]" id="receive_action" class="form-control"  onchange="get_select_desc(this, 1)">
                    <?php foreach($action as $k=>$v){ ?>
                        <option value="<?php echo $v['code'];?>" <?php echo $res['receive_action'] == $v['code'] ? "selected" : "";?> data-desc="<?php echo $v["desc"];?>"><?php echo $v["name"];?></option>
                    <?php } ?>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">领任务操作id</label>
            <div class="col-sm-2">
                <input type="text" name="label[receive_rule]" id="receive_rule" value="<?php echo $res['receive_rule'];?>" class="form-control" id="displayorder" placeholder="">
            </div>
            <div class="col-sm-8">
                <p class="help-block ling_desc" style="color: #ff0000"></p>
            </div>
        </div>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">完成任务操作<span class="b_red">*</span></label>
            <div class="col-sm-4">

                <select name="label[finish_action]" id="finish_action" class="form-control" onchange="get_select_desc(this, 0)">
                    <?php foreach($action as $k=>$v){ ?>
                        <option value="<?php echo $v['code'];?>" <?php echo $res['finish_action'] == $v['code'] ? "selected" : "";?> data-desc="<?php echo $v["desc"];?>"><?php echo $v["name"];?></option>
                    <?php } ?>
                </select>
            </div>
            <div class="col-sm-6">
                <p class="help-block">
<!--                    如果为带有子任务的主任务，此处必须选择为【完成所有子任务】-->
                </p>
            </div>
        </div>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">完成任务操作id<span class="b_red">*</span></label>
            <div class="col-sm-2">
                <input type="text" name="label[finish_rule]" id="finish_rule" value="<?php echo $res['finish_rule'];?>" class="form-control" id="displayorder" placeholder="">
            </div>
            <div class="col-sm-8">
                <p class="help-block wan_desc" style="color: #ff0000"></p>
            </div>
        </div>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">任务码<span class="b_red">*</span></label>
            <div class="col-sm-2">
                <input type="text" name="label[code]" id="code" value="<?php echo $res['code'];?>" class="form-control" id="displayorder" placeholder="task_code">
            </div>
            <div class="col-sm-8">
                <p class="help-block">必须英文,内容为空表示在用户任务列表显示，不为空，用户任务列表不显示 如：task_code</p>
            </div>
        </div>


        <div class="form-group">
            <label for="sex" class="col-sm-2 control-label">跳转方式<span class="b_red">*</span></label>
            <div class="col-md-4">
                <label><input type="radio" name="label[jump_type]" id="jump_type_0" value="0" <?php echo empty($res['jump_type']) || $res['jump_type'] == 0 ? 'checked' : ''; ?>/> 弹窗</label>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <label><input type="radio" name="label[jump_type]" id="jump_type_1" value="1" <?php echo $res['jump_type'] == 1 ? 'checked' : ''; ?>/> 跳转</label>
            </div>
            <div class="col-sm-3">
                <p class="help-block">不能为空，app端按钮使用</p>
            </div>
        </div>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">提示图片/跳转位置<span class="b_red">*</span></label>
            <div class="col-sm-5">
                <input type="text" name="label[location]" id="location" value="<?php echo $res["location"];?>" class="form-control" id="location" placeholder="">
            </div>
            <div class="col-sm-5">
                <p class="help-block">如果是弹窗方式时此处填写图片地址(img_lib/a150148732747338.png)<br>如果是跳转方式时此处为页面id(TaskPage)前端来定</p>
            </div>
        </div>
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">内容开始高度</label>
            <div class="col-sm-5">
                <input type="text" name="label[margin_top]" id="short_desc" value="<?php echo $res["margin_top"];?>" class="form-control" id="margin_top" placeholder="内容开始高度">
            </div>
            <div class="col-sm-5">
                <p class="help-block">内容开始高度，可以为空，只有在跳转方式为弹窗时生效</p>
            </div>
        </div>


        <div class="form-group">
            <label for="sex" class="col-sm-2 control-label">领奖方式<span class="b_red">*</span></label>
            <div class="col-md-4">
                <label><input type="radio" name="label[is_auto_receive]" id="is_auto_receive_0" value="0" <?php echo empty($res['is_auto_receive']) || $res['is_auto_receive'] == 0 ? 'checked' : ''; ?>/> 手动领奖</label>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <label><input type="radio" name="label[is_auto_receive]" id="is_auto_receive_1" value="1" <?php echo $res['is_auto_receive'] == 1 ? 'checked' : ''; ?>/> 自动领奖</label>
            </div>
            <div class="col-sm-3">
                <p class="help-block">自动领取不能选择实物奖品作为奖励，请遵守规则</p>
            </div>
        </div>
        <div class="form-group">
            <label for="sex" class="col-sm-2 control-label">任务分类<span class="b_red">*</span></label>
            <div class="col-md-8">
                <label><input type="radio" name="label[category]" id="category_0" value="0" <?php echo empty($res['category']) || $res['category'] == 0 ? 'checked' : ''; ?>/> 新手任务</label>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <label><input type="radio" name="label[category]" id="category_1" value="1" <?php echo $res['category'] == 1 ? 'checked' : ''; ?>/> 日常任务</label>
            </div>

        </div>
        <div class="form-group">
            <label for="sex" class="col-sm-2 control-label">前端列表是否展示<span class="b_red">*</span></label>
            <div class="col-md-8">
                <label><input type="radio" name="label[show]" id="show_1" value="1" <?php echo empty($res['show']) || $res['show'] == 1 ? 'checked' : ''; ?>/> 展示</label>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <label><input type="radio" name="label[show]" id="show_2" value="2" <?php echo $res['show'] == 2 ? 'checked' : ''; ?>/> 不展示</label>
            </div>

        </div>

        <div class="form-group">
            <label for="sex" class="col-sm-2 control-label">任务周期<span class="b_red">*</span></label>
            <div class="col-md-5">
                <?php
                    $task_cycle_arr=$this->config->config['task_cycle'];
                    foreach($task_cycle_arr as $k=>$v){
                ?>
                <label><input type="radio" name="label[cycle]" id="cycle_<?php echo $k;?>" value="<?php echo $k;?>" <?php echo  $res['cycle'] == $k ? 'checked' : ''; ?>/> <?php echo $v;?></label>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <?php }?>
            </div>
            <div class="col-sm-3">
                <p class="help-block">不能为空，app端按钮使用</p>
            </div>
        </div>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">周期内循环次数<span class="b_red">*</span></label>
            <div class="col-sm-2">
                <input type="text" name="label[num]" id="num" value="<?php echo empty($res['num'])?0:$res['num'];?>" class="form-control" id="displayorder" placeholder="周期内循环次数">
            </div>
            <div class="col-sm-8">
                <p class="help-block">不能为空，0为不限次数</p>
            </div>
        </div>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">任务开始时间</label>
            <div class="col-sm-2">
                <div class='input-group date' id='datetimepicker1'>
                    <input type='text' name="label[start_time]" id="start_time" class="form-control" value="<?php echo empty($res["start_time"])?'':date('Y-m-d',$res["start_time"]);?>"/>
                                            <span class="input-group-addon">
                                            <span class="glyphicon glyphicon-calendar"></span>
                                        </span>
                </div>
            </div>
            <div class="col-sm-6">
                <p class="help-block">如果选择具体时间则为时间区间任务；如果为空，则表示任务开始时间不限制</p>
            </div>
        </div>


        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">任务有效时长<span class="b_red">*</span></label>
            <!--需要从时间戳转成天数-->
            <div class="col-sm-1">
                <input type="text" name="time[d]" id="time_d" value="<?php echo empty($res['time']['d'])?0:$res['time']['d'];?>" class="form-control" id="displayorder" placeholder="天">

            </div>
            <div class="col-sm-1" style="width: 11px; margin:5px 0px; padding: 0px;">
                天
            </div>
            <div class="col-sm-1">
                <input type="text" name="time[h]" id="time_h" value="<?php echo empty($res['time']['h'])?0:$res['time']['h'];?>" class="form-control" id="displayorder" placeholder="小时">
            </div>

            <div class="col-sm-1" style="width: 40px; margin:5px 0px; padding: 0px;">
                小时
            </div>
            <div class="col-sm-1">
                <input type="text" name="time[s]" id="time_h" value="<?php echo empty($res['time']['s'])?0:$res['time']['s'];?>" class="form-control" id="displayorder" placeholder="小时">
            </div>

            <div class="col-sm-1" style="width: 40px; margin:5px 0px; padding: 0px;">
                秒
            </div>
            <div class="col-sm-4">
                <p class="help-block">如果天和小时全为0，表示任务没有结束时间</p>
            </div>
        </div>


        <div class="form-group">
            <label for="sex" class="col-sm-2 control-label">奖励发放<span class="b_red">*</span></label>
            <div class="col-md-2">
                <label><input type="radio" name="label[is_give_out]" id="is_give_out_0" value="0" <?php echo empty($res['is_give_out']) || $res['jump_type'] == 0 ? 'checked' : ''; ?>/> 不发放</label>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <label><input type="radio" name="label[is_give_out]" id="is_give_out_1" value="1" <?php echo $res['is_give_out'] == 1 ? 'checked' : ''; ?>/>  发放</label>
            </div>
            <div class="col-sm-6">
                <p class="help-block">奖励是否真实发放，如果不发放，则只在用户端展示</p>
            </div>
        </div>


        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">奖品发放数量<span class="b_red">*</span></label>
            <div class="col-sm-2">
                <input type="text" name="label[prize_num]" id="prize_num" value="<?php echo empty($res['prize_num'])?0:$res['prize_num'];?>" class="form-control" id="displayorder" placeholder="奖品发放数量">
            </div>
            <div class="col-sm-8">
                <p class="help-block">奖品发放数量，不能为空，0发放所有奖品，1为发一件、2为发二件……</p>
            </div>
        </div>

        <!--奖励 采用原doctor/doctor_edit.php 其中标签修改后请测试是否正确 start-->
        <div class="form-group">
            <label for="items" class="col-sm-2 control-label">奖励选择<span class="b_red">*</span></label>
            <input type="hidden" name="prize[item]" id="prize_items" value='<?php echo $task_prize?>'/>
            <div class="col-sm-8" id="items">

                <select name="item_fid_1" id="item_fid_3" onchange="set_prize(this, 1)">
                    <option value="0">--请选择--</option>

                    <?php foreach($prize as $itemCategory){ ?>
                        <option value="<?php echo $itemCategory['id'];?>"><?php echo $itemCategory['name'];?></option>
                    <?php }?>
                </select>


                <div class="item_show" id="item_show">
                    <?php
                    $items = json_decode($task_prize,true) ? json_decode($task_prize,true) : array();
                    foreach($items as $item){
                        ?>
                        <div class="item_info">
                            <span class="close_d" onclick="setClose_prize(this,<?php echo $item['id'];?>)">x</span>
                            <span><?php echo $prize[$item['id']]['name'];?></span>
                            <div class="clearfix"></div>
                        </div>
                    <?php }?>
                </div>
                <div class="clearfix"></div>
            </div>
        </div>
        <!--奖励end-->

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label"><?php echo $this->config->config['prize_category'][0]['name']?>奖励<span class="b_red">*</span></label>
            <div class="col-sm-2">
                <input type="text" name="prize_integral" id="prize_integral" value="0" class="form-control" id="displayorder" placeholder="<?php echo $this->config->config['prize_category'][0]['name']?>数量">
            </div>
            <div class="col-sm-8">
                <p class="help-block"><?php echo $this->config->config['prize_category'][0]['name']?>奖励：输入后自动生成奖品，空和0为无操作</p>
            </div>
        </div>


        <div class="modal-footer">
            <button type="reset" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" onclick="on_submit()" class="btn btn-primary">保存</button>
        </div>

    </form>

    <script>
        $(function(){
            function getDate(num) {
                var t =  24*3600*1000;
                var n = new Date()*1;
                return new Date(n +  num*t).toLocaleDateString().replace(/\//g,'-');
            }
            function setDateRange(type) {
                var $start=$('#datetimepicker1 input');
                var $end=$('#datetimepicker2 input');
                switch(type){
                    case 'dm':
                        $start.val(getDate(-1*(new Date().getDate())+1));
                        $end.val(getDate(0));
                        break;
                    case 'd1':
                        $start.val(getDate(-1));
                        $end.val(getDate(-1));
                        break;
                    case 'd7':
                        $start.val(getDate(-7));
                        $end.val(getDate(0));
                        break;
                    case 'd30':
                        $start.val(getDate(-30));
                        $end.val(getDate(0));
                        break;
                }
            }
            $('#datetimepicker1,#datetimepicker2').datetimepicker({
                language: 'zh-CN',
                minView: "month",
                format:'yyyy-mm-dd',
                initialDate: new Date(),
                autoclose: true,
                todayBtn: true,
            });
            //setDateRange('dm');
            $(".time-range").bind('click',function () {
                setDateRange($(this).data('type'));
            })
        })
    </script>

</div>